// pages/home/home.js
var util = require('../../utils/util.js')
const db = wx.cloud.database()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list:[],
    initList:[],
    dateYTD:'',
    keyWords:'',
    value:'',
    radio:'',

    option1: [
      { text: '全部', value: 0 },
      { text: '丢失了', value: 1 },
      { text: '捡到了', value: 2 }
    ],
    option2: [
      { text: '时间从近到远', value: 'a' },
      { text: '时间从远到近', value: 'b' }
    ],
    radio2: 0,
    rank2: 'a',

    option3: [
      { text: '全部', value: 0 },
      { text: '出售', value: 1 },
      { text: '求购', value: 2 }
    ],
    option4: [
      { text: '时间从近到远', value: 'a' },
      { text: '时间从远到近', value: 'b' }
    ],
    radio3: 0,
    rank3: 'a'
  },

  toSearch() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  onChange(event) {
    console.log('tags',event.detail.index)
    this.setData({ 
      active: event.detail.index
    });
    //获取标签页数据数据
    if (this.data.active==0){
      db.collection('Issues').where({
        date: {
          $regex: this.data.dateYTD
        }
      })
        .get({
          success: res => {
            console.log('今日', this.data.dateYTD)
            this.setData({
              list: res.data
            })
          }
        });
    }else{
      db.collection('Issues').where({
        tags: this.data.active
      }).orderBy('timestamp', 'desc')
        .get({
          success: res => {
            console.log('其他的数据')
            this.setData({
              list: res.data,
              initList: res.data
            })
          }
        })
    }
  },

  onChangeRadio2(event) {
    this.setData({
      radio2: event.detail,
      radio: event.detail
    });
    if(this.data.radio==0){
      if(this.data.rank2=='b'){
        db.collection('Issues').where({
          tags: this.data.active
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('全部从远到近', this.data.radio, this.data.rank2)
              this.setData({
                list: res.data,
                initList: res.data
              })
            }
          })
      }else{
        console.log('全部从近到远',  this.data.radio, this.data.rank2)
        this.setData({
          list: this.data.initList
        })
      }
    }else{
      if (this.data.rank2 == 'b'){
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('从远到近', this.data.radio, this.data.rank2)
              this.setData({
                list: res.data
              })
            }
          });
      }else{
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'desc')
          .get({
            success: res => {
              console.log('从近到远',  this.data.radio, this.data.rank2)
              this.setData({
                list: res.data
              })
            }
          });
      }
    }
  },

  onChangeRank2(event) {
    console.log('改变rank',event.detail)
    this.setData({
      rank2: event.detail,
    });
    if (this.data.radio == 0){
      if (this.data.rank2 == 'b'){
        db.collection('Issues').where({
          tags: this.data.active,
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('全部从远到近', this.data.radio, this.data.rank2)
              this.setData({
                list: res.data
              })
            }
          });
      }else{
        console.log('全部从近到远',  this.data.radio, this.data.rank2)
        this.setData({
          list: this.data.initList
        })
      }
    }else{
      if (this.data.rank2 == 'b') {
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('从远到近', this.data.radio, this.data.rank2)
              this.setData({
                list: res.data
              })
            }
          });
      }else{
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'desc')
          .get({
            success: res => {
              console.log('从近到远', this.data.radio, this.data.rank2)
              this.setData({
                list: res.data
              })
            }
          });
      }
    }

  },

  onChangeRadio3(event) {
    this.setData({
      radio3: event.detail,
      radio: event.detail
    });
    if (this.data.radio == 0) {
      if (this.data.rank3 == 'b') {
        db.collection('Issues').where({
          tags: this.data.active
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('全部从远到近', this.data.radio, this.data.rank3)
              this.setData({
                list: res.data,
                initList: res.data
              })
            }
          })
      } else {
        console.log('全部从近到远', this.data.radio, this.data.rank3)
        this.setData({
          list: this.data.initList
        })
      }
    } else {
      if (this.data.rank3 == 'b') {
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('从远到近', this.data.radio, this.data.rank3)
              this.setData({
                list: res.data
              })
            }
          });
      } else {
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'desc')
          .get({
            success: res => {
              console.log('从近到远', this.data.radio, this.data.rank3)
              this.setData({
                list: res.data
              })
            }
          });
      }
    }
  },
  onChangeRank3(event) {
    console.log('改变rank', event.detail)
    this.setData({
      rank3: event.detail,
    });
    if (this.data.radio == 0) {
      if (this.data.rank3 == 'b') {
        db.collection('Issues').where({
          tags: this.data.active,
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('全部从远到近', this.data.radio, this.data.rank3)
              this.setData({
                list: res.data
              })
            }
          });
      } else {
        console.log('全部从近到远', this.data.radio, this.data.rank3)
        this.setData({
          list: this.data.initList
        })
      }
    } else {
      if (this.data.rank3 == 'b') {
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'asce')
          .get({
            success: res => {
              console.log('从远到近', this.data.radio, this.data.rank3)
              this.setData({
                list: res.data
              })
            }
          });
      } else {
        db.collection('Issues').where({
          tags: this.data.active,
          radio: this.data.radio
        }).orderBy('timestamp', 'desc')
          .get({
            success: res => {
              console.log('从近到远', this.data.radio, this.data.rank3)
              this.setData({
                list: res.data
              })
            }
          });
      }
    }
  },

  toDetail(e) {
    console.log(e)
    const issueId = e.currentTarget.id
    wx.navigateTo({
      url: '/pages/detail/detail?issueId=' + issueId
    })
  },

  addToWish(e){
    console.log(e)
    const { item } = e.currentTarget.dataset
    const i = app.globalData.wishList.findIndex(v => v._id == item._id)
    console.log(i)
    if (i > -1) {
      wx.showToast({
        title: '已收藏',
        image: '/images/alert_toast.png' 
      })
    } else {
      app.globalData.wishList.push(item)
      wx.setStorageSync('wishList', app.globalData.wishList)
      wx.showToast({
        title: '收藏成功',
        icon: 'success'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取今日日期（年月日）
    this.setData({
      dateYTD: util.formatYTD(new Date())
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //加载今日数据
    db.collection('Issues').where({
      date: {
        $regex: this.data.dateYTD
      }
    })
      .get({
        success: res => {
          console.log('加载，今日', this.data.dateYTD)
          this.setData({
            list: res.data
          })
        }
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})